package sdk.org.apache.shiro.G1_工具类;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.HashRequest;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.Sha1Hash;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.hash.Sha384Hash;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.SimpleByteSource;

import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;

public class C03_散列算法 {
    private static final String CHARSET_NAME = "UTF-8";
    
    private static void run(Timer timer) throws Exception {
        String str = "hello";
        String salt = "123";
        
        ConsoleUtils.log("原字符串", str);
        ConsoleUtils.log("使用shiro的Md5Hash工具编码.toString()", new Md5Hash(str).toString());
        ConsoleUtils.log("使用shiro的Md5Hash工具编码.toBase64()", new Md5Hash(str).toBase64());
        ConsoleUtils.log("   使用shiro的Md5Hash工具编码.toHex()", new Md5Hash(str).toHex());
        ConsoleUtils.log("使用shiro的Md5Hash工具编码.toString()", new Md5Hash(str, salt).toString());
        ConsoleUtils.log("使用shiro的Md5Hash工具编码.toBase64()", new Md5Hash(str, salt).toBase64());
        ConsoleUtils.log("   使用shiro的Md5Hash工具编码.toHex()", new Md5Hash(str, salt).toHex());
        
        ConsoleUtils.log("使用shiro的Sha1Hash工具编码.toString()", new Sha1Hash(str, salt).toString());
        ConsoleUtils.log("使用shiro的Sha1Hash工具编码.toBase64()", new Sha1Hash(str, salt).toBase64());
        ConsoleUtils.log("   使用shiro的Sha1Hash工具编码.toHex()", new Sha1Hash(str, salt).toHex());
        ConsoleUtils.log("使用shiro的Sha256Hash工具编码.toString()", new Sha256Hash(str, salt).toString());
        ConsoleUtils.log("使用shiro的Sha256Hash工具编码.toBase64()", new Sha256Hash(str, salt).toBase64());
        ConsoleUtils.log("   使用shiro的Sha256Hash工具编码.toHex()", new Sha256Hash(str, salt).toHex());
        ConsoleUtils.log("使用shiro的Sha384Hash工具编码.toString()", new Sha384Hash(str, salt).toString());
        ConsoleUtils.log("使用shiro的Sha384Hash工具编码.toBase64()", new Sha384Hash(str, salt).toBase64());
        ConsoleUtils.log("   使用shiro的Sha384Hash工具编码.toHex()", new Sha384Hash(str, salt).toHex());
        ConsoleUtils.log("使用shiro的Sha512Hash工具编码.toString()", new Sha512Hash(str, salt).toString());
        ConsoleUtils.log("使用shiro的Sha512Hash工具编码.toBase64()", new Sha512Hash(str, salt).toBase64());
        ConsoleUtils.log("   使用shiro的Sha512Hash工具编码.toHex()", new Sha512Hash(str, salt).toHex());
        
        DefaultHashService hashService = new DefaultHashService(); //默认算法SHA-512
        hashService.setHashAlgorithmName("SHA-512");
//        hashService.setPrivateSalt(new SimpleByteSource("123")); //私盐，默认无
        hashService.setGeneratePublicSalt(false);//是否生成公盐，默认false
//        hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());//用于生成公盐。默认就这个
        hashService.setHashIterations(2); //生成Hash值的迭代次数
        HashRequest request = new HashRequest.Builder()
                                 .setAlgorithmName("MD5")
                                 .setSource(ByteSource.Util.bytes(str))
                                 .setSalt(ByteSource.Util.bytes(salt))
                                 .setIterations(1)
                                 .build();
        ConsoleUtils.log("使用shiro的封装工具编码.toString()", hashService.computeHash(request).toString());
        ConsoleUtils.log("   使用shiro的封装工具编码.toHex()", hashService.computeHash(request).toHex());
        ConsoleUtils.log("使用shiro的封装工具编码.toBase64()", hashService.computeHash(request).toBase64());
        
        DefaultHashService hashService1 = new DefaultHashService();
        hashService1.setGeneratePublicSalt(false);
        HashRequest request1 = new HashRequest.Builder()
                .setAlgorithmName(Sha256Hash.ALGORITHM_NAME)
                .setSource(ByteSource.Util.bytes(str))
                .setSalt(null)
                .setIterations(1)
                .build();
        // Iterations=1     86fcb4c0551ea48ede7df5ed9626eee7
        // Iterations=2     c942f011ced5f36de066dd2d948538cb
        // Salt=null        5d41402abc4b2a76b9719d911017c592
        ConsoleUtils.log("  hashService1.toHex()", hashService1.computeHash(request1).toHex());
        
    }
    
    
    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            run(timer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print("执行总用时");
            System.exit(0);
        }
    }
}
